<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "350px"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      xLine:["07-01周六","07-02周日","07-03周一","07-04周二","07-05周三","07-06周四","07-07周五"],
      xData1:[13,31,51,41,56,12,51],
      xData2:[23,21,51,21,26,32,21],
      xData3:[23,31,31,21,16,12,21],
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({ expectedData, actualData } = {}) {
        var that = this;
      this.chart.setOption({
        xAxis: {
          type: "category",
          boundaryGap: true,
          data: that.xLine
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross"
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          },
          splitLine:false,
          splitArea:false,
        },
        series: [
          {
            name: "订单数",
            // smooth: 0.7,
            type: "bar",
            data: that.xData1,
            animationDuration: 1000,
            animationEasing: "quadraticOut"
          },
          {
            name: "订单数1",
            // smooth: 0.7,
            type: "bar",
            data: that.xData2,
            areaStyle:{
              color:'#f56c6c'
            },
            animationDuration: 1000,
            animationEasing: "quadraticOut"
          },
          {
            name: "订单数2",
            // smooth: 0.7,
            type: "bar",
            data: that.xData3,
            animationDuration: 1000,
            animationEasing: "quadraticOut"
          },
        ]
      });
    }
  }
};
</script>
